package main

import "fmt"

// Definition for singly-linked list.
type ListNode struct {
	Val  int
	Next *ListNode
}

func (ll *ListNode) Print() {
	if ll == nil {
		return
	}
	cur := ll
	for cur != nil {
		fmt.Printf("%d\t", cur.Val)
		cur = cur.Next
	}
	fmt.Println()
}

func removeElements(head *ListNode, val int) *ListNode {
	//1.定义虚拟头节点
	tmp := &ListNode{Val: -1} //虚拟头节点
	tmp.Next = head
	cur := tmp

	//2.删除节点
	for cur.Next != nil {
		if cur.Next.Val == val {
			cur.Next = cur.Next.Next //如果下个节点值与比较值相同，删除下个节点
		} else {
			cur = cur.Next //如果下个节点的值不相同，则移动指针变量
		}
	}
	return tmp.Next
}

func main() {
	head := &ListNode{Val: 1}
	head.Next = &ListNode{Val: 2}
	head.Next.Next = &ListNode{Val: 6}
	head.Next.Next.Next = &ListNode{Val: 3}
	head.Next.Next.Next.Next = &ListNode{Val: 4}
	head.Next.Next.Next.Next.Next = &ListNode{Val: 5}
	head.Next.Next.Next.Next.Next.Next = &ListNode{Val: 6}
	node := removeElements(head, 6)
	node.Print()

}
